package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.ArticleComment;
import com.mofang.powerdekorhelper.model.NewsInfoDetial;
import com.mofang.powerdekorhelper.model.ResultMessage4;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.NewsInfoDetialView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/7/21 0021.
 */

public class NewsInfoDetialPersenter extends BasePresent<NewsInfoDetialView> {

    RetrofitSerives retrofitSerives;

    Call<NewsInfoDetial> detialCall;

    Call<ArticleComment> commentCall;

    Call<ResultMessage4> qrCodeCall;

    public void getNewsInfoDetial(String id) {

        view.showProgress();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        JSONObject jsonParams = new JSONObject();
        try {
            jsonParams.put("id", id);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams.toString());
        detialCall = retrofitSerives.getNewsInfoDetails(body);

        detialCall.enqueue(new Callback<NewsInfoDetial>() {
            @Override
            public void onResponse(Call<NewsInfoDetial> call, Response<NewsInfoDetial> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {
                        view.setNewsInfoDetial(response.body());
                        view.hideProgress();
                    } else {
                        view.showErrorLayout(null);
                        view.toast(R.string.net_request_fail);
                    }
                } else {
                    view.showErrorLayout(null);
                    view.toast(R.string.net_request_fail);
                }
            }

            @Override
            public void onFailure(Call<NewsInfoDetial> call, Throwable t) {
                view.showErrorLayout(null);
                view.toast(R.string.net_request_abnormal);
            }
        });

    }

    public void getArticleComment(int page, int pageSize, String articleId) {

        view.showProgress();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.SellerAddressTest);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), getJsonParams(page, pageSize, articleId));
        commentCall = retrofitSerives.getArticleComment(body);
        commentCall.enqueue(new Callback<ArticleComment>() {
            @Override
            public void onResponse(Call<ArticleComment> call, Response<ArticleComment> response) {
                if (response.isSuccessful()) {

                    if (response.body().getCode().equals("0")) {
                        view.setArticleComment(response.body());
                        view.hideProgress();
                    } else {
                        view.showErrorLayout(null);
                        view.toast(R.string.net_request_fail);
                    }
                } else {
                    view.showErrorLayout(null);
                    view.toast(R.string.net_request_fail);
                }
            }

            @Override
            public void onFailure(Call<ArticleComment> call, Throwable t) {
                view.showErrorLayout(null);
                view.toast(R.string.net_request_fail);
            }
        });
    }

    public void getQrCode(String id, int factoryId, int type) {

        view.showProgress();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.SellerAddressTest);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), getParams(id, factoryId, type));

        qrCodeCall = retrofitSerives.getQrcode(body);

        qrCodeCall.enqueue(new Callback<ResultMessage4>() {
            @Override
            public void onResponse(Call<ResultMessage4> call, Response<ResultMessage4> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {

                        view.setQrCode(response.body());
                        view.hideProgress();
                    } else {
                        view.showErrorLayout(null);
                        view.toast(R.string.net_request_fail);
                    }
                } else {
                    view.showErrorLayout(null);
                    view.toast(R.string.net_request_fail);
                }
            }

            @Override
            public void onFailure(Call<ResultMessage4> call, Throwable t) {
                view.showErrorLayout(null);
                view.toast(R.string.net_request_fail);
            }
        });
    }

    private String getParams(String id, int factoryId, int type) {
        JSONObject paramsJson = new JSONObject();
        try {
            paramsJson.put("id", id);
            paramsJson.put("user_id", factoryId);
            paramsJson.put("type", type);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return paramsJson.toString();
    }

    private String getJsonParams(int page, int pageSize, String articleId) {

        JSONObject jsonParams = new JSONObject();

        try {
            jsonParams.put("page", page);
            jsonParams.put("pageSize", pageSize);
            JSONObject param = new JSONObject();
            param.put("article_id", articleId);
            jsonParams.put("param", param);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jsonParams.toString();
    }

    @Override
    public void stopRequest() {

    }
}